
package com.seekting.study;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

/**
 * LruCache特性:get先判断有没有,有就计数命中记数加1,没有会调到create方法,create成功,则进来
 */
public class LruTest {

    public static void main(String[] args) {

        LinkedHashMap<String, BBitmap> map = new LinkedHashMap<String, LruTest.BBitmap>(100, 0.75f,
                true);

        LruCache<String, BBitmap> lruCache = new LruCache<String, BBitmap>(10) {
            @Override
            protected long sizeOf(String key, BBitmap value) {
                System.out.println("key" + key);
                return value.size();
            }

            @Override
            protected void entryRemoved(boolean evicted, String key, BBitmap oldValue,
                    BBitmap newValue) {
                System.out.println("remove evicted " + evicted + " key " + key);
            }
        };

        lruCache.put("2", new BBitmap(2));
        lruCache.put("3", new BBitmap(3));
        lruCache.get("2");
        lruCache.put("6", new BBitmap(6));
        // lruCache.evictAll();
        map.put("2", new BBitmap(2));
        map.put("1", new BBitmap(1));
        map.put("3", new BBitmap(3));

        map.get("1");
        map.get("2");
        map.get("3");
        Iterator<Entry<String, BBitmap>> entry = map.entrySet().iterator();
        while (entry.hasNext()) {
            System.out.println(entry.next().getKey());
        }

    }

    private static class BBitmap {

        int size;

        public BBitmap(int size) {
            super();
            this.size = size;
        }

        public int size() {
            return size;
        }

    }
}
